﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Linq;

namespace SksApp
{
    public partial class MainView : Form
    {
        public MainView()
        {
            InitializeComponent();
            Timer tmr = new Timer();
            tmr.Interval = 1000;
            tmr.Tick += (ss, ee) =>
            {
                lblZaman.Text = DateTime.Now.toStr();
            };
            tmr.Enabled = true;

            this.Load += new EventHandler(MainView_Load);
        }

        void MainView_Load(object sender, EventArgs e)
        {
            LoginInit();
        }

        private void LoginInit()
        {
            Pinpad pad = new Pinpad();
            pad.Location = new Point(this.Width - 360, this.Height - 450);
            pad.BackColor = Color.Transparent;
            pad.MessageReceived += (ss) =>
                {
                    if (string.IsNullOrEmpty(ss))
                    {
                        MsgBox.Show(MessageStyle.Error, "Dikkat !", "Kasiyer şifresi boş geçilemez !");
                    }
                    else
                    {
                        using (SksEntity ent = new SksEntity())
                        {
                            Kasiyer ks = ent.Kasiyerler.Where(x => x.Sifresi == ss).FirstOrDefault();
                            if (ks != null)
                            {
                                Security.Get.AktifKasiyer = ks;
                                pad.Dispose();
                                this.Controls.Remove(pad);
                                MainMenuInit(null, null);
                            }
                            else
                            {
                                MsgBox.Show(MessageStyle.Error, "Dikkat !", "Şifre geçersiz !");
                            }
                        }
                    }
                };
            picContent.Controls.Add(pad);
        }
        private void MainMenuInit(object sender, EventArgs e)
        {
            MainMenu mn = new MainMenu();
            mn.Location = new Point((this.Width - mn.Width) / 2, (this.Height - mn.Height) / 2);
            mn.MessageReceived += (ss) =>
                {
                    switch (ss)
                    {
                        case MainMenu.Menu.Pos:
                            PosView view = new PosView();
                            view.Dock = DockStyle.Fill;
                            view.Event = MainMenuInit;
                            picContent.Controls.Add(view);
                            break;
                        case MainMenu.Menu.Urun:
                            ListView urn = new ListView();
                            urn.Location = new Point((this.Width - urn.Width) / 2, (this.Height - urn.Height) / 2);
                            urn.Event = MainMenuInit;

                            urn.ListeTipi = ListView.ListeTipleri.Stok;
                            picContent.Controls.Add(urn);
                            break;
                        case MainMenu.Menu.Cari:
                            ListView ms = new ListView();
                            ms.Location = new Point((this.Width - ms.Width) / 2, (this.Height - ms.Height) / 2);
                            ms.Event = MainMenuInit;
                            ms.ListeTipi = ListView.ListeTipleri.Musteri;
                            picContent.Controls.Add(ms);
                            break;
                        case MainMenu.Menu.Rapor:
                            break;
                        case MainMenu.Menu.Cikis:
                            LoginInit();
                            break;
                        case MainMenu.Menu.Ayarlar:
                            SettingsMenuInit(null, null);
                            break;
                        default:
                            break;
                    }
                    mn.Dispose();
                    this.Controls.Remove(mn);
                };
            picContent.Controls.Add(mn);
        }
        private void SettingsMenuInit(object sender, EventArgs e)
        {
            SettingsMenu mn = new SettingsMenu();
            mn.Event = MainMenuInit;
            mn.Width = 600;
            mn.Height = 600;
            mn.Location = new Point((this.Width - mn.Width) / 2, (this.Height - mn.Height) / 2);
            mn.MessageReceived += (ss) =>
            {
                switch (ss)
                {
                    case SettingsMenu.Menu.KasaTanim:
                        KasaTanimView ks = new KasaTanimView();
                        ks.Location = new Point((this.Width / 2) - (ks.Width / 2), (this.Height / 2) - (ks.Height / 2));
                        ks.Event = SettingsMenuInit;
                        picContent.Controls.Add(ks);
                        break;
                    case SettingsMenu.Menu.KasiyerTanim:
                        KasiyerTanitimView kr = new KasiyerTanitimView();
                        kr.Location = new Point((this.Width / 2) - (kr.Width / 2), (this.Height / 2) - (kr.Height / 2));
                        kr.Event = SettingsMenuInit;
                        picContent.Controls.Add(kr);
                        break;
                    case SettingsMenu.Menu.KasaYetki:
                        break;
                    case SettingsMenu.Menu.KasiyerYetki:
                        break;
                    case SettingsMenu.Menu.MagazaTanim:
                        MagazaTanimView mg = new MagazaTanimView();
                        mg.Location = new Point((this.Width / 2) - (mg.Width / 2), (this.Height / 2) - (mg.Height / 2));
                        mg.Event = SettingsMenuInit;
                        picContent.Controls.Add(mg);
                        break;
                    case SettingsMenu.Menu.VergiTanim:
                        VergiTanimView kdv = new VergiTanimView();
                        kdv.Location = new Point((this.Width / 2) - (kdv.Width / 2), (this.Height / 2) - (kdv.Height / 2));
                        kdv.Event = SettingsMenuInit;
                        picContent.Controls.Add(kdv);
                        break;
                    case SettingsMenu.Menu.OdemeTipi:
                        OdemeView odm = new OdemeView();
                        odm.Location = new Point((this.Width / 2) - (odm.Width / 2), (this.Height / 2) - (odm.Height / 2));
                        odm.Event = SettingsMenuInit;
                        picContent.Controls.Add(odm);
                        break;
                    case SettingsMenu.Menu.EvrakTasarim:
                        EvrakTasarimView tsr = new EvrakTasarimView();
                        tsr.Location = new Point((this.Width / 2) - (tsr.Width / 2), (this.Height / 2) - (tsr.Height / 2));
                        tsr.Event = SettingsMenuInit;
                        picContent.Controls.Add(tsr);
                        break;
                    case SettingsMenu.Menu.UrunGroup:
                        ProductGroupView pr = new ProductGroupView();
                        pr.Location = new Point((this.Width / 2) - (pr.Width / 2), (this.Height / 2) - (pr.Height / 2));
                        pr.Event = SettingsMenuInit;
                        picContent.Controls.Add(pr);
                        break;
                    case SettingsMenu.Menu.SistemAyarlari:
                        SettingsView set = new SettingsView();
                        set.Location = new Point((this.Width / 2) - (set.Width / 2), (this.Height / 2) - (set.Height / 2));
                        set.Event = SettingsMenuInit;
                        picContent.Controls.Add(set);
                        break;
                }
                mn.Dispose();
                this.Controls.Remove(mn);
            };
            picContent.Controls.Add(mn);
        }
    }
}
